home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / mozilla-firefox / include / string / nsStringIterator.h < prev    next >
C/C++ Source or Header  |  2006-05-08  |  10KB  |  378 lines

  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Mozilla Public License Version
  6.  * 1.1 (the "License"); you may not use this file except in compliance with
  7.  * the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/MPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is Mozilla.
  16.  *
  17.  * The Initial Developer of the Original Code is
  18.  * Netscape Communications.
  19.  * Portions created by the Initial Developer are Copyright (C) 2001
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *   Scott Collins <scc@mozilla.org> (original author)
  24.  *
  25.  * Alternatively, the contents of this file may be used under the terms of
  26.  * either of the GNU General Public License Version 2 or later (the "GPL"),
  27.  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28.  * in which case the provisions of the GPL or the LGPL are applicable instead
  29.  * of those above. If you wish to allow use of your version of this file only
  30.  * under the terms of either the GPL or the LGPL, and not to allow others to
  31.  * use your version of this file under the terms of the MPL, indicate your
  32.  * decision by deleting the provisions above and replace them with the notice
  33.  * and other provisions required by the GPL or the LGPL. If you do not delete
  34.  * the provisions above, a recipient may use your version of this file under
  35.  * the terms of any one of the MPL, the GPL or the LGPL.
  36.  *
  37.  * ***** END LICENSE BLOCK ***** */
  38.  
  39. #ifndef nsStringIterator_h___
  40. #define nsStringIterator_h___
  41.  
  42. #ifndef nsCharTraits_h___
  43. #include "nsCharTraits.h"
  44. #endif
  45.  
  46. #ifndef nsAlgorithm_h___
  47. #include "nsAlgorithm.h"
  48. #endif
  49.  
  50. #ifndef nsDebug_h___
  51. #include "nsDebug.h"
  52. #endif
  53.  
  54.   /**
  55.    * @see nsTAString
  56.    */
  57.  
  58. template <class CharT>
  59. class nsReadingIterator
  60.   {
  61.     public:
  62.       typedef nsReadingIterator<CharT>    self_type;
  63.       typedef ptrdiff_t                   difference_type;
  64.       typedef CharT                       value_type;
  65.       typedef const CharT*                pointer;
  66.       typedef const CharT&                reference;
  67.  
  68.     private:
  69.       friend class nsAString;
  70.       friend class nsACString;
  71. #ifdef MOZ_V1_STRING_ABI
  72.       friend class nsSubstring;
  73.       friend class nsCSubstring;
  74. #endif
  75.  
  76.         // unfortunately, the API for nsReadingIterator requires that the
  77.         // iterator know its start and end positions.  this was needed when
  78.         // we supported multi-fragment strings, but now it is really just
  79.         // extra baggage.  we should remove mStart and mEnd at some point.
  80.  
  81.       const CharT* mStart;
  82.       const CharT* mEnd;
  83.       const CharT* mPosition;
  84.  
  85.     public:
  86.       nsReadingIterator() { }
  87.       // nsReadingIterator( const nsReadingIterator<CharT>& );                    // auto-generated copy-constructor OK
  88.       // nsReadingIterator<CharT>& operator=( const nsReadingIterator<CharT>& );  // auto-generated copy-assignment operator OK
  89.  
  90.       inline void normalize_forward() {}
  91.       inline void normalize_backward() {}
  92.  
  93.       pointer
  94.       start() const
  95.         {
  96.           return mStart;
  97.         }
  98.  
  99.       pointer
  100.       end() const
  101.         {
  102.           return mEnd;
  103.         }
  104.  
  105.       pointer
  106.       get() const
  107.         {
  108.           return mPosition;
  109.         }
  110.       
  111.       CharT
  112.       operator*() const
  113.         {
  114.           return *get();
  115.         }
  116.  
  117. #if 0
  118.         // An iterator really deserves this, but some compilers (notably IBM VisualAge for OS/2)
  119.         //  don't like this when |CharT| is a type without members.
  120.       pointer
  121.       operator->() const
  122.         {
  123.           return get();
  124.         }
  125. #endif
  126.  
  127.       self_type&
  128.       operator++()
  129.         {
  130.           ++mPosition;
  131.           return *this;
  132.         }
  133.  
  134.       self_type
  135.       operator++( int )
  136.         {
  137.           self_type result(*this);
  138.           ++mPosition;
  139.           return result;
  140.         }
  141.  
  142.       self_type&
  143.       operator--()
  144.         {
  145.           --mPosition;
  146.           return *this;
  147.         }
  148.  
  149.       self_type
  150.       operator--( int )
  151.         {
  152.           self_type result(*this);
  153.           --mPosition;
  154.           return result;
  155.         }
  156.  
  157.       difference_type
  158.       size_forward() const
  159.         {
  160.           return mEnd - mPosition;
  161.         }
  162.  
  163.       difference_type
  164.       size_backward() const
  165.         {
  166.           return mPosition - mStart;
  167.         }
  168.  
  169.       self_type&
  170.       advance( difference_type n )
  171.         {
  172.           if (n > 0)
  173.             {
  174.               difference_type step = NS_MIN(n, size_forward());
  175.  
  176.               NS_ASSERTION(step>0, "can't advance a reading iterator beyond the end of a string");
  177.  
  178.               mPosition += step;
  179.             }
  180.           else if (n < 0)
  181.             {
  182.               difference_type step = NS_MAX(n, -size_backward());
  183.  
  184.               NS_ASSERTION(step<0, "can't advance (backward) a reading iterator beyond the end of a string");
  185.  
  186.               mPosition += step;
  187.             }
  188.           return *this;
  189.         }
  190.   };
  191.  
  192.   /**
  193.    * @see nsTAString
  194.    */
  195.  
  196. template <class CharT>
  197. class nsWritingIterator
  198.   {
  199.     public:
  200.       typedef nsWritingIterator<CharT>   self_type;
  201.       typedef ptrdiff_t                  difference_type;
  202.       typedef CharT                      value_type;
  203.       typedef CharT*                     pointer;
  204.       typedef CharT&                     reference;
  205.  
  206.     private:
  207.       friend class nsAString;
  208.       friend class nsACString;
  209. #ifdef MOZ_V1_STRING_ABI
  210.       friend class nsSubstring;
  211.       friend class nsCSubstring;
  212. #endif
  213.  
  214.         // unfortunately, the API for nsWritingIterator requires that the
  215.         // iterator know its start and end positions.  this was needed when
  216.         // we supported multi-fragment strings, but now it is really just
  217.         // extra baggage.  we should remove mStart and mEnd at some point.
  218.  
  219.       CharT* mStart;
  220.       CharT* mEnd;
  221.       CharT* mPosition;
  222.  
  223.     public:
  224.       nsWritingIterator() { }
  225.       // nsWritingIterator( const nsWritingIterator<CharT>& );                    // auto-generated copy-constructor OK
  226.       // nsWritingIterator<CharT>& operator=( const nsWritingIterator<CharT>& );  // auto-generated copy-assignment operator OK
  227.  
  228.       inline void normalize_forward() {}
  229.       inline void normalize_backward() {}
  230.  
  231.       pointer
  232.       start() const
  233.         {
  234.           return mStart;
  235.         }
  236.  
  237.       pointer
  238.       end() const
  239.         {
  240.           return mEnd;
  241.         }
  242.  
  243.       pointer
  244.       get() const
  245.         {
  246.           return mPosition;
  247.         }
  248.       
  249.       reference
  250.       operator*() const
  251.         {
  252.           return *get();
  253.         }
  254.  
  255. #if 0
  256.         // An iterator really deserves this, but some compilers (notably IBM VisualAge for OS/2)
  257.         //  don't like this when |CharT| is a type without members.
  258.       pointer
  259.       operator->() const
  260.         {
  261.           return get();
  262.         }
  263. #endif
  264.  
  265.       self_type&
  266.       operator++()
  267.         {
  268.           ++mPosition;
  269.           return *this;
  270.         }
  271.  
  272.       self_type
  273.       operator++( int )
  274.         {
  275.           self_type result(*this);
  276.           ++mPosition;
  277.           return result;
  278.         }
  279.  
  280.       self_type&
  281.       operator--()
  282.         {
  283.           --mPosition;
  284.           return *this;
  285.         }
  286.  
  287.       self_type
  288.       operator--( int )
  289.         {
  290.           self_type result(*this);
  291.           --mPosition;
  292.           return result;
  293.         }
  294.  
  295.       difference_type
  296.       size_forward() const
  297.         {
  298.           return mEnd - mPosition;
  299.         }
  300.  
  301.       difference_type
  302.       size_backward() const
  303.         {
  304.           return mPosition - mStart;
  305.         }
  306.  
  307.       self_type&
  308.       advance( difference_type n )
  309.         {
  310.           if (n > 0)
  311.             {
  312.               difference_type step = NS_MIN(n, size_forward());
  313.  
  314.               NS_ASSERTION(step>0, "can't advance a writing iterator beyond the end of a string");
  315.  
  316.               mPosition += step;
  317.             }
  318.           else if (n < 0)
  319.             {
  320.               difference_type step = NS_MAX(n, -size_backward());
  321.  
  322.               NS_ASSERTION(step<0, "can't advance (backward) a writing iterator beyond the end of a string");
  323.  
  324.               mPosition += step;
  325.             }
  326.           return *this;
  327.         }
  328.  
  329.       PRUint32
  330.       write( const value_type* s, PRUint32 n )
  331.         {
  332.           NS_ASSERTION(size_forward() > 0, "You can't |write| into an |nsWritingIterator| with no space!");
  333.  
  334.           nsCharTraits<value_type>::move(mPosition, s, n);
  335.           advance( difference_type(n) );
  336.           return n;
  337.         }
  338.   };
  339.  
  340. template <class CharT>
  341. inline
  342. PRBool
  343. operator==( const nsReadingIterator<CharT>& lhs, const nsReadingIterator<CharT>& rhs )
  344.   {
  345.     return lhs.get() == rhs.get();
  346.   }
  347.  
  348. template <class CharT>
  349. inline
  350. PRBool
  351. operator!=( const nsReadingIterator<CharT>& lhs, const nsReadingIterator<CharT>& rhs )
  352.   {
  353.     return lhs.get() != rhs.get();
  354.   }
  355.  
  356.  
  357.   //
  358.   // |nsWritingIterator|s
  359.   //
  360.  
  361. template <class CharT>
  362. inline
  363. PRBool
  364. operator==( const nsWritingIterator<CharT>& lhs, const nsWritingIterator<CharT>& rhs )
  365.   {
  366.     return lhs.get() == rhs.get();
  367.   }
  368.  
  369. template <class CharT>
  370. inline
  371. PRBool
  372. operator!=( const nsWritingIterator<CharT>& lhs, const nsWritingIterator<CharT>& rhs )
  373.   {
  374.     return lhs.get() != rhs.get();
  375.   }
  376.  
  377. #endif /* !defined(nsStringIterator_h___) */
  378.